﻿using System;
using System.Collections.Generic;
using System.Web;

namespace Utility
{
    public class ErrorLog
    {
        private ErrorLog() { }

        public static ErrorLog Instance = new ErrorLog();

        public void WriteWCFSuccess(string strClassName, string strFunctionName, string strStoredProcedureName, IDictionary<string, object> formParams)
        {
            LogHelper.Instance.WriteEventLog("WCF:" + strClassName, LogTypeEnum.systemSuccess);
            LogHelper.Instance.WriteEventLog("\tFunction:" + strFunctionName, LogTypeEnum.systemSuccess);
            LogHelper.Instance.WriteEventLog("\t\tStored Procedure:" + strStoredProcedureName, LogTypeEnum.systemSuccess);
            LogHelper.Instance.WriteEventLog("\t\t\tParams:", LogTypeEnum.systemSuccess);
            if (formParams != null)
                foreach (string key in formParams.Keys)
                    LogHelper.Instance.WriteEventLog("\t\t\t\t" + key + ":" + formParams[key] + "", LogTypeEnum.systemSuccess);

            LogHelper.Instance.WriteEventLog("----------------------------------------------------------------------------------------", LogTypeEnum.systemSuccess);
        }

        public void WriteCacheSuccess(string cacheName, string comment)
        {
            LogHelper.Instance.WriteEventLog("Cache name:" + cacheName, LogTypeEnum.cache);
            LogHelper.Instance.WriteEventLog("\tComment:" + comment, LogTypeEnum.cache);

            LogHelper.Instance.WriteEventLog("----------------------------------------------------------------------------------------", LogTypeEnum.cache);
        }

        public void WriteWCFError(string strClassName, string strFunctionName, string strStoredProcedureName, IDictionary<string, object> formParams, Exception ex)
        {
            LogHelper.Instance.WriteEventLog("WCF:" + strClassName, LogTypeEnum.system);
            LogHelper.Instance.WriteEventLog("Error StackTrace:" + ex.StackTrace, LogTypeEnum.system);
            LogHelper.Instance.WriteEventLog("\tFunction:" + strFunctionName, LogTypeEnum.system);
            LogHelper.Instance.WriteEventLog("\t\tStored Procedure:" + strStoredProcedureName, LogTypeEnum.system);
            LogHelper.Instance.WriteEventLog("\t\t\tParams:", LogTypeEnum.system);
            if (formParams != null)
                foreach (string key in formParams.Keys)
                    LogHelper.Instance.WriteEventLog("\t\t\t\t" + key + ":" + formParams[key] + "", LogTypeEnum.system);

            LogHelper.Instance.WriteEventLog("Error Message:" + ex.Message, LogTypeEnum.system);
            LogHelper.Instance.WriteEventLog("Error InnerException:" + ex.InnerException, LogTypeEnum.system);

            LogHelper.Instance.WriteEventLog("----------------------------------------------------------------------------------------", LogTypeEnum.system);
        }

        public void WriteDataAccessError(string strClassName, string strFunctionName, string strStoredProcedureName, IDictionary<string, object> formParams, Exception ex)
        {
            LogHelper.Instance.WriteEventLog("DataAccess:" + strClassName, LogTypeEnum.sqlDataAccess);
            LogHelper.Instance.WriteEventLog("Error StackTrace:" + ex.StackTrace, LogTypeEnum.system);
            LogHelper.Instance.WriteEventLog("\tFunction:" + strFunctionName, LogTypeEnum.sqlDataAccess);
            LogHelper.Instance.WriteEventLog("\t\tStored Procedure:" + strStoredProcedureName, LogTypeEnum.sqlDataAccess);
            LogHelper.Instance.WriteEventLog("\t\t\tParams:", LogTypeEnum.sqlDataAccess);
            if (formParams != null)
                foreach (string key in formParams.Keys)
                    LogHelper.Instance.WriteEventLog("\t\t\t\t" + key + ":" + formParams[key] + "", LogTypeEnum.sqlDataAccess);

            LogHelper.Instance.WriteEventLog("Error Message:" + ex.Message, LogTypeEnum.sqlDataAccess);
            LogHelper.Instance.WriteEventLog("Error InnerException:" + ex.InnerException, LogTypeEnum.sqlDataAccess);

            LogHelper.Instance.WriteEventLog("----------------------------------------------------------------------------------------", LogTypeEnum.sqlDataAccess);
        }

        public void WriteBusinessLogicError(string strClassName, string strFunctionName, string strJsonHealperFun, IDictionary<string, object> formParams, IDictionary<string, object> outputParams, Exception ex)
        {
            LogHelper.Instance.WriteEventLog("BusinessLogic:" + strClassName, LogTypeEnum.sqlBusinessLogic);
            LogHelper.Instance.WriteEventLog("Error StackTrace:" + ex.StackTrace, LogTypeEnum.system);
            LogHelper.Instance.WriteEventLog("\tFunction:" + strFunctionName, LogTypeEnum.sqlBusinessLogic);
            LogHelper.Instance.WriteEventLog("\t\tJSONHelper:" + strJsonHealperFun, LogTypeEnum.sqlBusinessLogic);
            LogHelper.Instance.WriteEventLog("\t\t\tIn Params:", LogTypeEnum.sqlBusinessLogic);
            if (formParams != null)
                foreach (string key in formParams.Keys)
                    LogHelper.Instance.WriteEventLog("\t\t\t\t" + key + ":" + formParams[key] + "", LogTypeEnum.sqlBusinessLogic);
            LogHelper.Instance.WriteEventLog("\t\t\tOut Params:", LogTypeEnum.sqlBusinessLogic);
            if (outputParams != null)
                foreach (string key in outputParams.Keys)
                    LogHelper.Instance.WriteEventLog("\t\t\t\t" + key + ":" + outputParams[key] + "", LogTypeEnum.sqlBusinessLogic);

            LogHelper.Instance.WriteEventLog("Error Message:" + ex.Message, LogTypeEnum.sqlBusinessLogic);
            LogHelper.Instance.WriteEventLog("Error InnerException:" + ex.InnerException, LogTypeEnum.sqlBusinessLogic);

            LogHelper.Instance.WriteEventLog("----------------------------------------------------------------------------------------", LogTypeEnum.sqlBusinessLogic);
        }

        public void WriteHandlerError(string strClassName, HttpContext Context, IDictionary<string, object> formParams, Exception ex)
        {
            LogHelper.Instance.WriteEventLog("Handler:" + strClassName, LogTypeEnum.sqlHandler);
            LogHelper.Instance.WriteEventLog("Error StackTrace:" + ex.StackTrace, LogTypeEnum.system);
            LogHelper.Instance.WriteEventLog("\tUrl:" + Context.Request.Url, LogTypeEnum.sqlHandler);
            LogHelper.Instance.WriteEventLog("\tUserHostAddress:" + Context.Request.UserHostAddress, LogTypeEnum.sqlHandler);
            LogHelper.Instance.WriteEventLog("\tUserHostName:" + Context.Request.UserHostName, LogTypeEnum.sqlHandler);
            LogHelper.Instance.WriteEventLog("\tUserAgent:" + Context.Request.UserAgent, LogTypeEnum.sqlHandler);
            LogHelper.Instance.WriteEventLog("\tQueryString:" + Context.Request.QueryString, LogTypeEnum.sqlHandler);
            LogHelper.Instance.WriteEventLog("\tParams:", LogTypeEnum.sqlHandler);
            if (formParams != null)
                foreach (string key in formParams.Keys)
                    LogHelper.Instance.WriteEventLog("\t\t" + key + ":" + formParams[key] + "", LogTypeEnum.sqlHandler);

            LogHelper.Instance.WriteEventLog("\tSession Params:", LogTypeEnum.sqlHandler);
            if (Context.Session != null)
                foreach (string key in Context.Session.Keys)
                    LogHelper.Instance.WriteEventLog("\t\t" + key + ":" + Context.Session[key] + "", LogTypeEnum.sqlHandler);

            LogHelper.Instance.WriteEventLog("Error Message:" + ex.Message, LogTypeEnum.sqlHandler);
            LogHelper.Instance.WriteEventLog("Error InnerException:" + ex.InnerException, LogTypeEnum.sqlHandler);

            LogHelper.Instance.WriteEventLog("----------------------------------------------------------------------------------------", LogTypeEnum.sqlHandler);
        }

        public void WriteSendError( Exception ex)
        {
            LogHelper.Instance.WriteEventLog("Error Message:" + ex.Message, LogTypeEnum.system);
            LogHelper.Instance.WriteEventLog("Error StackTrace:" + ex.StackTrace, LogTypeEnum.system);
            LogHelper.Instance.WriteEventLog("\tParams:", LogTypeEnum.system);

            LogHelper.Instance.WriteEventLog("\tSession Params:", LogTypeEnum.system);
            LogHelper.Instance.WriteEventLog("Error InnerException:" + ex.InnerException, LogTypeEnum.system);

            LogHelper.Instance.WriteEventLog("----------------------------------------------------------------------------------------", LogTypeEnum.system);
        }
    }
}